import Vue from 'vue'
import Vuex from 'vuex'
import axios from "axios";
import {cloneObject} from "../utils/common";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isLogin : false,
    userType:'',
    info:{},
    messageLength : 0
  },
  getters:{
    getMessageLength : state => {
      if(state.messageLength === 0){
        state.messageLength = JSON.parse(sessionStorage.getItem('messageLength'))
      }
      if( state.messageLength === 0)return ''
      else return  state.messageLength
    },
    getIsLogin : state => {
      console.log(sessionStorage.getItem('isLogin'))
     if(sessionStorage.getItem('isLogin') == '1'){
       state.isLogin = true
     }
     else if(sessionStorage.getItem('isLogin') == '0'){
       state.isLogin = false
     }
     return state.isLogin
    },
    getInfo : state => {
     if(JSON.stringify(state.info) == '{}'){
       state.info = JSON.parse(sessionStorage.getItem('info'))
       console.log('tttttt')
     }
     return state.info
    },
    getUserType:state => {
      if(!state.userType){
        state.userType = sessionStorage.getItem('userType')
      }
      return state.userType
    }
  },
  mutations: {
    setIsLogin(state,payload)
    {
      this.state.isLogin = payload.isLogin
    },
    setInfo(state,payload)
    {
      this.state.info = payload.info
    },
    setUserType(state,payload)
    {
      this.state.userType = payload.userType
    },
    setMessageLength(state,payload)
    {
      this.state.messageLength = payload.messageLength
    }
  },
  actions: {
    getMessageLength(context){
      //获取未读消息个数
          let id ;
          let url;
          if( context.getters.getUserType == 'server'){
            id = context.getters.getInfo.ser_id
            url = 'https://haotry.top/api/serviceSide/getUnreadNews'

            axios.post(url, JSON.stringify({ser_id:id}), {
              headers: {
                'Content-Type': 'application/json'
              }
            }).then(res => {
              let data = res.data
              console.log('订单个数')
              console.log(data)
              if (data.status === 200) {
                 sessionStorage.setItem('messageLength',data.data.length)
                context.commit('setMessageLength',{messageLength : data.data.length})
              }
              else{
                alert('获取未读信息长度失败！')
              }
            })
          }
          else{
            id = context.getters.getInfo.ber_id
            url = 'https://haotry.top/api/buyer/getNumOfUnread'
            console.log('采买方id')
            console.log(id)

            axios.post(url, JSON.stringify({ber_id : id}), {
              headers: {
                'Content-Type': 'application/json'
              }
            }).then(res => {
              let data = res.data
              console.log('订单个数')
              if (data.status === 200) {
                 sessionStorage.setItem('messageLength',data.data)
                 context.commit('setMessageLength',{messageLength : data.data})
              }
              else{
                alert('获取未读信息长度失败！')
              }
            })
          }

    }
  },
  modules: {
  }
})
